/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.soccer.cms.modules.cms.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.soccer.cms.common.config.Global;
import com.soccer.cms.common.persistence.Page;
import com.soccer.cms.common.web.BaseController;
import com.soccer.cms.common.utils.StringUtils;
import com.soccer.cms.modules.cms.entity.Racevideo;
import com.soccer.cms.modules.cms.service.RacevideoService;

/**
 * 赛事视频Controller
 * @author zouqh
 * @version 2015-05-21
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/racevideo")
public class RacevideoController extends BaseController {

	@Autowired
	private RacevideoService racevideoService;
	
	@ModelAttribute
	public Racevideo get(@RequestParam(required=false) String id) {
		Racevideo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = racevideoService.get(id);
		}
		if (entity == null){
			entity = new Racevideo();
		}
		return entity;
	}
	
	@RequiresPermissions("cms:racevideo:view")
	@RequestMapping(value = {"index"})
	public String indexPage(Racevideo racevideo, HttpServletRequest request, HttpServletResponse response, Model model) {
		//Page<Racevideo> page = racevideoService.findPage(new Page<Racevideo>(request, response), racevideo); 
		//model.addAttribute("page", page);
		return "modules/cms/racevideoIndex";
	}
	
	@RequiresPermissions("cms:racevideo:view")
	@RequestMapping(value = {"list", ""})
	public String list(Racevideo racevideo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Racevideo> page = racevideoService.findPage(new Page<Racevideo>(request, response), racevideo); 
		model.addAttribute("page", page);
		return "modules/cms/racevideoList";
	}

	@RequiresPermissions("cms:racevideo:view")
	@RequestMapping(value = "form")
	public String form(Racevideo racevideo, Model model) {
		
		model.addAttribute("racevideo", racevideo);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt",request.getParameter("opt"));
		return "modules/cms/racevideoForm";
	}

	@RequiresPermissions("cms:racevideo:edit")
	@RequestMapping(value = "save")
	public String save(Racevideo racevideo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, racevideo)){
			return form(racevideo, model);
		}
		racevideoService.save(racevideo);
		//redirectAttributes.addFlashAttribute("id", racevideo.getId());
		//addMessage(redirectAttributes, "保存赛事视频成功");
		//return "redirect:"+Global.getAdminPath()+"/cms/racevideo/form?repage";
		model.addAttribute("racevideo", racevideo);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		model.addAttribute("opt","save");
		model.addAttribute("message","保存赛事视频成功");
		return "modules/cms/racevideoForm";
	}
	
	@RequiresPermissions("cms:racevideo:edit")
	@RequestMapping(value = "delete")
	public String delete(Racevideo racevideo, RedirectAttributes redirectAttributes) {
		racevideoService.delete(racevideo);
		addMessage(redirectAttributes, "删除赛事视频成功");
		return "redirect:"+Global.getAdminPath()+"/cms/racevideo/?repage";
	}
	
	@RequiresPermissions("cms:racevideo:edit")
	@RequestMapping(value = "batchDelete")
	public String delete(String ids, RedirectAttributes redirectAttributes) {
		racevideoService.delete(ids);
		addMessage(redirectAttributes, "删除赛事视频成功");
		return "redirect:"+Global.getAdminPath()+"/cms/racevideo/?repage";
	}

}